/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.gis.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hopes.bizz.basic.common.constant.enums.ParkBasicSysDictEnum;
import com.hopes.bizz.basic.drive.service.BaseDriveStopRecordService;
import com.hopes.bizz.basic.drive.service.BaseOverhaulRecordService;
import com.hopes.bizz.basic.gis.service.BaseGisService;
import com.hopes.bizz.basic.gis.vo.*;
import com.hopes.bizz.basic.hazard.process.service.HazProcessService;
import com.hopes.bizz.basic.hazard.process.vo.HazProcessTypeGisVo;
import com.hopes.bizz.basic.hazard.source.service.HazHazardSourceService;
import com.hopes.bizz.basic.hazard.source.vo.HazHazardSourceGisVO;
import com.hopes.bizz.basic.park.entity.BaseParkPlanEntity;
import com.hopes.bizz.basic.park.entity.BaseParkSafeManagerEntity;
import com.hopes.bizz.basic.park.entity.BaseSafeManageMaterialEntity;
import com.hopes.bizz.basic.park.entity.BaseShiftQueueEntity;
import com.hopes.bizz.basic.park.service.*;
import com.hopes.bizz.basic.park.vo.AccidentEventGisVo;
import com.hopes.bizz.basic.safe.service.BaseLicenceProductionService;
import com.hopes.bizz.basic.safe.service.BaseThreeMeantimeService;
import com.hopes.bizz.basic.safe.vo.BaseThreeMeantimeGisVo;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 企业事故事件
 *
 * @author pig
 * @date 2023-10-25 14:43:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/gis")
@Tag(description = "gis", name = "安全基础一张图")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BaseGisController {

    private final BaseParkPlanService baseParkPlanService;
    private final BaseSafeManageMaterialService baseSafeManageMaterialService;
    private final BaseParkSafeManagerService baseParkSafeManagerService;
    private final BaseParkForbiddenControlService baseParkForbiddenControlService;
    private final HazHazardSourceService hazHazardSourceService;
    private final HazProcessService hazProcessService;
    private final BaseAccidentEventService baseAccidentEventService;
    private final BaseThreeMeantimeService baseThreeMeantimeService;
    private final BaseGisService baseGisService;
    private final BaseLicenceProductionService baseLicenceProductionService;
    private final BaseDriveStopRecordService baseDriveStopRecordService;
    private final BaseOverhaulRecordService baseOverhaulRecordService;
    private final BaseShiftQueueService baseShiftQueueService;

    /**
     * 查看园区规划
     *
     * @return R
     */
    @Operation(summary = "查看园区规划", description = "查看园区规划")
    @SysLog("查看园区规划")
    @GetMapping("plan")
    public R queryPlan() {
        return R.ok(baseParkPlanService.lambdaQuery().orderByAsc(BaseParkPlanEntity::getEffectiveDate).list());
    }


    /**
     * 查看园区体系管理
     *
     * @return R
     */
    @Operation(summary = "查看园区体系管理", description = "查看园区体系管理")
    @SysLog("查看园区体系管理")
    @GetMapping("safeManageMaterial")
    public R querySafeManageMaterial() {
        return R.ok(baseSafeManageMaterialService.lambdaQuery().eq(BaseSafeManageMaterialEntity::getShowFlag, Boolean.TRUE).one());
    }


    /**
     * 从业人员统计
     *
     * @return R
     */
    @Operation(summary = "从业人员统计", description = "从业人员统计")
    @SysLog("从业人员统计")
    @GetMapping("safeManager")
    public R querySafeManager() {
        SafeManagerVo safeManagerVo = new SafeManagerVo();
        Map<String, Long> managerTypeMap = baseParkSafeManagerService.list().stream().collect(Collectors.groupingBy(BaseParkSafeManagerEntity::getManagerType, Collectors.counting()));
        //柱状图
        ArrayList<String> name = new ArrayList<>();
        ArrayList<Long> count = new ArrayList<>();
        JSONObject barData = new JSONObject();
        barData.put("name", name);
        barData.put("count", count);
        // 饼状图
        JSONArray pieData = new JSONArray(managerTypeMap.size());
        managerTypeMap.forEach((k, v) -> {
            HashMap<String, Object> data = new HashMap<>();
            data.put("name", DictResolver.getItemLabelByValue(ParkBasicSysDictEnum.manager_type, k));
            data.put("value", v);
            name.add(DictResolver.getItemLabelByValue(ParkBasicSysDictEnum.manager_type, k));
            count.add(v);
            pieData.add(data);
        });
        safeManagerVo.setPieData(pieData);
        safeManagerVo.setBarData(barData);
        return R.ok(safeManagerVo);
    }


    /**
     * 禁限控目录
     *
     * @return R
     */
    @Operation(summary = "禁限控目录", description = "禁限控目录")
    @SysLog("禁限控目录")
    @GetMapping("forbiddenControl")
    public R queryForbiddenControl() {
        ForbiddenControlVo forbiddenControlVo = baseParkForbiddenControlService.queryForbiddenControl();
        return R.ok(forbiddenControlVo);
    }

    /**
     * 重大危险源级别统计
     *
     * @return R
     */
    @Operation(summary = "重大危险源级别统计", description = "重大危险源级别统计")
    @SysLog("重大危险源级别统计")
    @GetMapping("hazardSourceLevel")
    public R<HazHazardSourceGisVO> getHazardSourceLevel() {
        return R.ok(hazHazardSourceService.getHazardSourceLevel());
    }

    /**
     * 重点监管化工工艺
     *
     * @return R
     */
    @Operation(summary = "重点监管化工工艺", description = "重点监管化工工艺")
    @SysLog("重点监管化工工艺")
    @GetMapping("hazProcessType")
    public R<HazProcessTypeGisVo> getHazProcessType() {
        return R.ok(hazProcessService.getHazProcessType());
    }

    /**
     * 意外事故
     *
     * @return R
     */
    @Operation(summary = "意外事故", description = "意外事故")
    @SysLog("意外事故")
    @GetMapping("accidentEvent")
    public R<AccidentEventGisVo> getAccidentEvent() {
        return R.ok(baseAccidentEventService.getAccidentEvent());
    }

    /**
     * 三同时统计
     *
     * @return R
     */
    @Operation(summary = "三同时统计", description = "三同时统计")
    @SysLog("三同时统计")
    @GetMapping("threeMeantime")
    public R<BaseThreeMeantimeGisVo> getThreeMeantime() {
        return R.ok(baseThreeMeantimeService.getThreeMeantime());
    }


    /**
     * 第三方管理统计
     *
     * @return R
     */
    @Operation(summary = "第三方管理统计", description = "第三方管理统计")
    @SysLog("第三方管理统计")
    @GetMapping("thirdInfo")
    public R<ThirdInfoVo> getThirdInfo() {
        return R.ok(baseGisService.getThirdInfo());
    }

    /**
     * 第三方纪录统计
     *
     * @return R
     */
    @Operation(summary = "第三方纪录统计", description = "第三方纪录统计")
    @SysLog("第三方纪录统计")
    @GetMapping("thirdRecord")
    public R<ThirdRecordVo> getThirdRecord() {
        return R.ok(baseGisService.getThirdRecord());
    }


    /**
     * 安全生产许可证统计
     *
     * @return R
     */
    @Operation(summary = "安全生产许可证统计", description = "安全生产许可证统计")
    @SysLog("安全生产许可证状态统计")
    @GetMapping("licenceProductionInfo")
    public R<LicenceProductionVo> getLicenceProductionInfo() {
        return R.ok(baseLicenceProductionService.getLicenceProductionInfo());
    }


    /**
     * 开停车统计
     *
     * @return R
     */
    @Operation(summary = "开停车统计", description = "开停车统计")
    @SysLog("开停车统计")
    @GetMapping("driveStopRecord")
    public R<DriveStopRecordVo> getDriveStopRecord() {
        return R.ok(baseDriveStopRecordService.getDriveStopRecord());
    }

    /**
     * 大检修统计
     *
     * @return R
     */
    @Operation(summary = "大检修统计", description = "大检修统计")
    @SysLog("大检修统计")
    @GetMapping("overhaulRecord")
    public R<OverhaulRecord> getOverhaulRecord() {
        return R.ok(baseOverhaulRecordService.getOverhaulRecord());
    }


    /**
     * 当日值守人员
     *
     * @return R
     */
    @Operation(summary = "当日值守人员", description = "当日值守人员")
    @SysLog("当日值守人员")
    @GetMapping("todayOnDuty")
    public R<List<TodayOnDutyVo>> getTodayOnDuty() {
        List<TodayOnDutyVo> onDutyVoList = baseShiftQueueService.lambdaQuery().eq(BaseShiftQueueEntity::getShiftDate, DateUtil.beginOfDay(new Date())).list().stream().map(item -> {
            TodayOnDutyVo todayOnDutyVo = new TodayOnDutyVo();
            todayOnDutyVo.setName(item.getShiftUserIdStr());
            todayOnDutyVo.setDuty(item.getPost());
            return todayOnDutyVo;
        }).collect(Collectors.toList());
        return R.ok(onDutyVoList);
    }


}
